import unittest
import requests
import os
import json
from parameterized import parameterized
import HTMLTestRunner


def test_data():
    json_path=os.path.dirname(__file__) + "\login.json"
    print(json_path)
    data=[]
    with open(json_path,encoding="utf-8") as f:
        json_data=json.load(f)
        for i in json_data:
            username=i.get("user")
            psd=i.get("pwd")
            data.append((username,psd))
    return data
class Testlogin(unittest.TestCase):
    def setUp(self):
        self.host="http://192.168.1.1/cgi-bin/luci"
        self.session=requests.Session()
        # 创建一个会话
    def tearDown(self):
        self.session.close()
    @parameterized.expand(test_data)
    def test01(self,username,psd):
        req=self.session.post(url=self.host,data={"username":username,"psd":psd})
        self.assertIn("process_message",req.text)

if __name__ == "__main__":
    file_path=os.path.dirname(__file__) + "\\test_result.html"
    with open(file_path,"wb") as f:
        runner=HTMLTestRunner(stream=f,title="test_api_result")
        suit=unittest.TestSuite()
        suit.addTest(unittest.makeSuite(Testlogin))
        runner.run(suit)

    # unittest.main()